package fr.oz.mobile.ui.profile 
{
	import caurina.transitions.Tweener;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import fr.oz.core.utils.Utils;
	import fr.oz.gui.profile.ControlBarColor;
	/**
	 * ...
	 * @author Djim
	 */
	public class ControlBar extends Sprite
	{
		private var controlBar:ControlBarColor;
		private var _gradientBar:Sprite;
		
		private var posXBegin:int = 109;
		private var posXEnd:int = 450;
		private var _length:int = 329;
		public var colorBegin:uint = 0xFF0000;
		public var colorEnd:uint = 0x0000FF;
		private var _totalMinut:Number;
		private var bg_gradient:Sprite;
		private var _initMinut:Number;
		private var _initHour:Number;
		
		public function ControlBar(hour:int, minut:int) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			_totalMinut = hour * 60 + minut;
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			bg_gradient = new Sprite();
			bg_gradient.graphics.beginFill(0xffffff, .1);
			bg_gradient.graphics.lineStyle(2, 0xffffff);
			bg_gradient.graphics.drawRect(0,0,480,80);
			bg_gradient.graphics.endFill();
			addChild(bg_gradient);
			bg_gradient.buttonMode = true;
			
			_gradientBar = new Sprite();
			addChild(_gradientBar);
			//myMask = new CurveMask_MC();
			//addChild(myMask);
			//_gradientBar.mask = myMask;
			_gradientBar.mouseEnabled = false;
			drawGradient();
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			removeChild(bg_gradient);
			bg_gradient = null;
			
			removeChild(_gradientBar);
			_gradientBar = null;
		}
		
		private function drawGradient():void
		{
			var fillType:String = GradientType.LINEAR;
			var colors:Array = [colorBegin, colorEnd];
			var alphas:Array = [1, 1];
			var ratios:Array = [0, 255];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(480, 0, 0, posXBegin);
			//matr.createGradientBox(480, 10, 0, 10);
			var spreadMethod:String = SpreadMethod.PAD;
			_gradientBar.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);  
			_gradientBar.graphics.drawRect(0,0,480,80);
			_gradientBar.graphics.endFill();
		}
		
		public function setColor(value:uint, begin:Boolean = true):void {
			if (begin)	colorBegin = value;
			else	colorEnd = value;
			drawGradient();
		}
		
		public function setPosX(value:int, begin:Boolean = true):void { 
		
			if (begin)	posXBegin = value - 75;
			else	posXEnd = value - 75;
			_length = posXEnd - posXBegin;
			drawGradient();
		}
		
		public function whatTimeItIs(percent:Number = 0):String {
			var minutSup:int = Math.floor(percent / 100 * 30);
			_initMinut = _totalMinut - 30 + minutSup;
			_initHour = Math.floor(_initMinut / 60);
			_initMinut = _initMinut % 60;
			
			return ""+Utils.addZero(String(_initHour))+":"+Utils.addZero(String(_initMinut));
		}
		
		public function get totalMinut():Number { return _totalMinut; }
	}

}